<template>
  <div class="file-preview-container">
    <div class="file-selector">
      <input
        type="file"
        ref="fileInput"
        @change="handleFileChange"
        accept=".doc,.docx,.xls,.xlsx,.pdf,.ppt,.pptx"
        class="hidden-input"
      />
      <button @click="triggerFileInput" class="select-btn">选择文件</button>
      <span v-if="selectedFile" class="file-name">{{ selectedFile.name }}</span>
    </div>

    <div v-if="loading" class="loading">
      <div class="spinner"></div>
      <span>文件加载中...</span>
    </div>

    <div v-if="error" class="error-message">
      {{ error }}
    </div>

    <!-- 根据文件类型动态加载对应的预览组件 -->
    <div v-if="!loading && !error && fileType" class="preview-container">
      <component
        :is="currentViewer"
        :file="selectedFile"
        :file-data="fileData"
        :fileType="fileType"
        @error="handleViewerError"
      ></component>
    </div>
  </div>
</template>

<script>
import PDFViewer from './PDFViewer.vue';
import WordViewer from './WordViewer.vue';
import ExcelViewer from './ExcelViewer.vue';
import PowerPointViewer from './PowerPointViewer.vue';

export default {
  name: 'FilePreview',
  components: {
    PDFViewer,
    WordViewer,
    ExcelViewer,
    PowerPointViewer
  },
  data() {
    return {
      selectedFile: null,
      fileType: null,
      loading: false,
      error: null,
      fileData: null
    };
  },
  computed: {
    currentViewer() {
      switch (this.fileType) {
        case 'pdf':
          return 'PDFViewer';
        case 'doc':
        case 'docx':
          return 'WordViewer';
        case 'xls':
        case 'xlsx':
          return 'ExcelViewer';
        case 'ppt':
        case 'pptx':
          return 'PowerPointViewer';
        default:
          return null;
      }
    }
  },
  methods: {
    triggerFileInput() {
      this.$refs.fileInput.click();
    },

    handleFileChange(event) {
      const file = event.target.files[0];
      if (!file) return;

      this.selectedFile = file;
      this.loading = true;
      this.error = null;
      this.fileType = this.getFileExtension(file.name);

      // 读取文件数据
      const fileReader = new FileReader();

      fileReader.onload = (event) => {
        this.fileData = event.target.result;
        this.loading = false;
      };

      fileReader.onerror = () => {
        this.error = '文件读取失败';
        this.loading = false;
      };

      fileReader.readAsArrayBuffer(file);
    },

    getFileExtension(filename) {
      return filename.split('.').pop().toLowerCase();
    },

    handleViewerError(errorMessage) {
      this.error = errorMessage;
    }
  }
};
</script>

<style scoped>
.file-preview-container {
  font-family: Arial, sans-serif;
  max-width: 1200px;
  margin: 0 auto;
  padding: 20px;
}

.file-selector {
  margin-bottom: 20px;
  display: flex;
  align-items: center;
}

.hidden-input {
  display: none;
}

.select-btn {
  background-color: #4CAF50;
  color: white;
  padding: 10px 15px;
  border: none;
  border-radius: 4px;
  cursor: pointer;
  font-size: 14px;
}

.select-btn:hover {
  background-color: #45a049;
}

.file-name {
  margin-left: 10px;
  font-size: 14px;
}

.loading {
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 20px;
}

.spinner {
  border: 4px solid #f3f3f3;
  border-top: 4px solid #3498db;
  border-radius: 50%;
  width: 30px;
  height: 30px;
  animation: spin 2s linear infinite;
  margin-right: 10px;
}

@keyframes spin {
  0% { transform: rotate(0deg); }
  100% { transform: rotate(360deg); }
}

.error-message {
  color: #f44336;
  padding: 10px;
  margin-bottom: 15px;
  border: 1px solid #f44336;
  border-radius: 4px;
  background-color: #ffebee;
}

.preview-container {
  border: 1px solid #ddd;
  border-radius: 4px;
  padding: 20px;
  min-height: 500px;
}
</style>